home *** CD-ROM | disk | FTP | other *** search
- function PROT_SandboxManager(iterator, listWarden, tableName) {
- this.debugZone = "sandboxMgr";
- this.iterator_ = iterator;
- this.dataImportedAtLeastOnce_ = false;
- var table = new PROT_TRTable(tableName);
- this.populateSandbox({ 'table': table });
- listWarden.registerSandboxUpdate(tableName,
- BindToObject(this.populateSandbox, this));
- listWarden.enableSandboxUpdates();
- }
- PROT_SandboxManager.prototype.checkHandler_ = function(handler) {
- if (!(handler instanceof Array)) {
- G_Debug(this, "handler isn't array");
- return false;
- }
- if (handler.length != 3) {
- G_Debug(this, "handler doesn't have 3 elements");
- return false;
- }
- if (typeof handler[0] != "string") {
- G_Debug(this, "callback isn't a string");
- return false;
- }
- if (typeof handler[1] != "string") {
- G_Debug(this, "eventType isn't a string");
- return false;
- }
- if (!(handler[2] instanceof Array)) {
- G_Debug(this, "attrs isn't an array");
- return false;
- }
- for (var i = 0; i < handler[2].length; i++) {
- if (typeof handler[2][i] != "string") {
- G_Debug(this, "attr " + handler[2][i] + " isn't a string");
- return false;
- }
- }
- return true;
- }
- PROT_SandboxManager.prototype.checkHandlers_ = function(handlers) {
- if (!(handlers instanceof Array))
- return false;
- for (var i = 0; i < handlers.length; i++) {
- if (!this.checkHandler_(handlers[i]))
- return false;
- }
- return true;
- }
- PROT_SandboxManager.prototype.registerHandlers_ = function() {
- var handlers = null;
- try {
- handlers = this.copyFromSandbox_(
- Components.utils.evalInSandbox("getHandlers();", this.sandbox_));
- } catch(e) {
- G_Debug(this, "Couldn't call getHandlers in sandbox " +
- "because of exception: " + e);
- return false;
- }
- if (!this.checkHandlers_(handlers)) {
- G_Debug(this, "checkHandlers failed! aborting");
- return false;
- }
- for (var i = 0; i < handlers.length; i++) {
- this.registerHandler_(handlers[i]);
- }
- return true;
- }
- PROT_SandboxManager.prototype.registerHandler_ = function(handler) {
- var F = this.createWrapper_(handler[0]);
- this.iterator_.registerEvent(handler[1], handler[2]);
- this.iterator_.registerHandler(handler[1], F);
- }
- PROT_SandboxManager.prototype.createWrapper_ = function(callback) {
- var F = function(e) {
- var evalString = callback + uneval(e) + ';';
- var results = null;
- try {
- results = this.copyFromSandbox_(
- Components.utils.evalInSandbox(evalString,
- this.sandbox_));
- } catch(e) {
- G_Debug(this, "Couldn't call " + evalString +
- " in sandbox because of exception " + e);
- return; // Don't call e.callback
- }
- if (!!e.callback && typeof e.callback == "function") {
- e.callback(results);
- }
- }
- return BindToObject(F, this);
- }
- PROT_SandboxManager.prototype.copyToSandbox_ = function(name, e) {
- var s = name + "=" + uneval(e);
- Components.utils.evalInSandbox(s, this.sandbox_);
- }
- PROT_SandboxManager.prototype.copyFromSandbox_ = function(e) {
- return eval(uneval(e));
- }
- PROT_SandboxManager.prototype.hasData = function() {
- return this.dataImportedAtLeastOnce_;
- }
- PROT_SandboxManager.prototype.populateSandbox = function(e) {
- if (!e.table) {
- G_Debug(this, "No data with which to populate sandbox!");
- return false;
- }
- this.iterator_.removeHandlers();
- this.sandbox_ = new Components.utils.Sandbox("example.com");
- var sandboxText = e.table.find("sandbox");
- if (!sandboxText) {
- G_Debug(this, "Bad value for sandbox key in sandbox table: " + sandboxText);
- return false;
- }
- sandboxText = decodeURIComponent(sandboxText);
- try {
- Components.utils.evalInSandbox(sandboxText, this.sandbox_);
- } catch(e) {
- G_Debug(this, "Couldn't load sandbox file, as a result of exception: " + e);
- return false;
- }
- this.dataImportedAtLeastOnce_ = this.registerHandlers_();
- return true;
- }
-